
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class LocalHtmlPage extends StatefulWidget {
  const LocalHtmlPage({Key? key}) : super(key: key);
  @override
  _LocalHtmlPageState createState() => _LocalHtmlPageState();
}

class _LocalHtmlPageState extends State<LocalHtmlPage> {
  InAppWebViewController? _controller;
  Uint8List? screenshot = Uint8List.fromList("111".codeUnits);
  bool iShow = true;

  Future<WebUri> _localUri() async {
    final dir = await getApplicationDocumentsDirectory();
    final file = File('${dir.path}/files/index.html');
    return WebUri('file://${file.path}');
  }
  void _incrementCounter()  async{
    var screenshotConfiguration = ScreenshotConfiguration(
        compressFormat: CompressFormat.JPEG,
        quality: 20,
        rect: InAppWebViewRect(width: 100, height: 100, x: 50, y: 50));
    if( _controller != null){
      var data = await _controller?.takeScreenshot();
      this.screenshot = data;
      setState(() {
        iShow = ! iShow;
      });
    }

  }
  CookieManager cookieManager = CookieManager.instance();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('本地 HTML')),
      body: Column(children: [
       Expanded(child:  FutureBuilder<WebUri>(
         future: _localUri(),
         builder: (context, snap) {
           if (!snap.hasData) return const Center(child: CircularProgressIndicator());
           final uri = snap.data!;
           return InAppWebView(
             initialUrlRequest: URLRequest(url:WebUri('https://www.baidu.com')),             
             onWebViewCreated: (c) async{
               _controller  = c ;
             },
           );
         },
       ),),
       Visibility(child: Text("照片"),visible: iShow,replacement: Container(
         child: Image.memory(this.screenshot!),
         width: 50,
         height: 50,
       ),)
      ],),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing
    );
  }
}
